package com.share.example.spider.schedule;

import com.share.example.spider.pipeline.Stockpipeline;
import com.share.example.spider.processor.StockProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class StockSchedule {

    @Autowired
    private Stockpipeline stockpipeline;

    @PostConstruct
    @Scheduled(cron = "0 0/10 * * * ?")
    public void pullStock() {
        log.info("开始异步提交任务");
        Spider spider = Spider.create(new StockProcessor());/*这里只能用new*/
        spider.addUrl("http://www.txsec.com/inc1/gpdm.asp");//任务的地址

        spider.addPipeline(stockpipeline);
        spider.thread(1);
        spider.setExitWhenComplete(true);
        spider.start();
        log.info("异步任务提交结束!");
    }
}
